/*
 * SemProjektGUIView.java
 */
package UILayer;

import ControlLayer.CustomerPickedEvent;
import ControlLayer.GlobalInstances;
import ControlLayer.ICustomerPickedEventListener;
import ControlLayer.ILoginStateChangedEventListener;
import ControlLayer.LoginStateChangedEvent;
import ModelLayer.Actor;
import UILayer.Screens.FindCustomerUI;
import UILayer.Screens.FindOrderUI;
import UILayer.Screens.FindSaleUI;
import UILayer.Screens.StartPanel;
import UILayer.Screens.OrderCarUI;
import UILayer.Screens.SellCarUI;
import UILayer.Screens.StatisticUI;
import UILayer.Screens.loginPanel;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.plaf.basic.BasicButtonUI;

/**
 * The application's main frame.
 */
public class MainGuiView extends FrameView
{
    JPopupMenu menu;
    Timer dropDownTimer;
    final MainGuiView mainView = this;

    public MainGuiView(SingleFrameApplication app)
    {
        super(app);

        initComponents();

        Image icon = Toolkit.getDefaultToolkit().getImage(this.getFrame().getClass().getResource("/UILayer/resources/arrow.png"));
        this.getFrame().setIconImage(icon);
        this.getFrame().setTitle("Uggerhøj Biler");

        menu = new JPopupMenu();

        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++)
        {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener()
        {
            public void propertyChange(java.beans.PropertyChangeEvent evt)
            {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName))
                {
                    if (!busyIconTimer.isRunning())
                    {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                }
                else if ("done".equals(propertyName))
                {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                }
                else if ("message".equals(propertyName))
                {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                }
                else if ("progress".equals(propertyName))
                {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });

        AddPane(new StartPanel(), "Start");

        GlobalInstances.getInstance().addILoginStateChangedEventListener(new ILoginStateChangedEventListener()
        {
            public void LoginStateChangedEventOccurred(LoginStateChangedEvent evt)
            {
                if (evt.getLoginState() == LoginStateChangedEvent.LoggedInState)
                {
                    SetUserLoggedInView(evt.getEmployee());

                }
                else
                {
                    SetUserLoggedOutView();
                }
            }
        });

        SetUserLoggedOutView();

        GlobalInstances.getInstance().addICustomerPickedEventListener(new ICustomerPickedEventListener() {

            public void CustomerPickedEventOccurred(CustomerPickedEvent evt)
            {
                statusMessageLabel.setText(evt.getCustomer().getName() + " er valgt som kunde");
            }
        });
    }

    private void SetUserLoggedOutView()
    {
        lblLoginStatus.setText("Log ind");
        lblLoginName.setText("Du er ikke logget ind");
        miShowLoginPanel.setText("Log ind");
    }

    private void SetUserLoggedInView(Actor actor)
    {
        lblLoginStatus.setText("Log ud");
        miShowLoginPanel.setText("Log ud");
        if (actor != null)
        {
            lblLoginName.setText(String.format("%s er logget ind", actor.getName()));
        }
    }

    public JTabbedPane getTabbedPanel()
    {
        return tabbedMainPanel;
    }

    @Action
    public void showAboutBox()
    {
        if (aboutBox == null)
        {
            JFrame mainFrame = MainGuiApp.getApplication().getMainFrame();
            aboutBox = new MainGuiAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        MainGuiApp.getApplication().show(aboutBox);
    }
    //menu.popup.show(mainPanel, 10, 10);
    int counter = 0;

    public void AddPane(Component panel, String title)
    {
        tabbedMainPanel.addTab(null, new JScrollPane(panel));

        TabButton tabCloseButton = new TabButton();
        tabCloseButton.setActionCommand(String.valueOf(counter));

        ActionListener al;
        al = new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                JButton btn = (JButton) ae.getSource();
                String s1 = btn.getActionCommand();
                for (int i = 1; i < tabbedMainPanel.getTabCount(); i++)
                {
                    JPanel pnl = (JPanel) tabbedMainPanel.getTabComponentAt(i);

                    btn = (JButton) pnl.getComponent(1);

                    String s2 = btn.getActionCommand();
                    if (s1.equals(s2))
                    {
                        tabbedMainPanel.removeTabAt(i);
                        break;
                    }
                }
            }
        };
        tabCloseButton.addActionListener(al);

        //
        JLabel label = new JLabel(title);
        JPanel tabPanel = new JPanel();
        tabPanel.setOpaque(false);
        tabPanel.add(label);
        if (counter > 0)
        {
            tabPanel.add(tabCloseButton);
        }
        tabbedMainPanel.setTabComponentAt(tabbedMainPanel.getTabCount() - 1, tabPanel);
        tabbedMainPanel.setSelectedIndex(tabbedMainPanel.getTabCount() - 1);
        //

        counter++;
    }

    private class TabButton extends JButton implements ActionListener
    {
        public TabButton()
        {
            int size = 20;
            setPreferredSize(new Dimension(size, size));
            setToolTipText("Close this tab");
            //Make the button looks the same for all Laf's
            setUI(new BasicButtonUI());
            //Make it transparent
            setContentAreaFilled(false);
            //No need to be focusable
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            //Making nice rollover effect
            //we use the same listener for all buttons
            addMouseListener(buttonMouseListener);
            setRolloverEnabled(true);
            //Close the proper tab by clicking the button
            addActionListener(this);
        }

        public void actionPerformed(ActionEvent e)
        {
            int i = tabbedMainPanel.indexOfTabComponent(TabButton.this);
            if (i != -1)
            {
                tabbedMainPanel.remove(i);
            }
        }

        //we don't want to update UI for this button
        public void updateUI()
        {
        }

        //paint the cross
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            //shift the image for pressed buttons
            if (getModel().isPressed())
            {
                g2.translate(1, 1);
            }
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.BLACK);
            if (getModel().isRollover())
            {
                g2.setColor(new Color(50, 250, 50));
            }
            int delta = 6;
            g2.drawLine(delta, delta, getWidth() - delta - 1, getHeight() - delta - 1);
            g2.drawLine(getWidth() - delta - 1, delta, delta, getHeight() - delta - 1);
            g2.dispose();
        }
    }
    private final static MouseListener buttonMouseListener = new MouseAdapter()
    {
        public void mouseEntered(MouseEvent e)
        {
            Component component = e.getComponent();
            if (component instanceof AbstractButton)
            {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(true);
            }
        }

        public void mouseExited(MouseEvent e)
        {
            Component component = e.getComponent();
            if (component instanceof AbstractButton)
            {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(false);
            }
        }
    };

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        tabbedMainPanel = new javax.swing.JTabbedPane();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        btnSale = new javax.swing.JButton();
        btnOrder = new javax.swing.JButton();
        btnStock = new javax.swing.JButton();
        btnCustomers = new javax.swing.JButton();
        btnStatistics = new javax.swing.JButton();
        lblLoginName = new javax.swing.JLabel();
        lblLoginStatus = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem btnFindCustomer = new javax.swing.JMenuItem();
        miShowLoginPanel = new javax.swing.JMenuItem();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mainPanelMouseClicked(evt);
            }
        });

        tabbedMainPanel.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(MainGuiView.class);
        tabbedMainPanel.setFont(resourceMap.getFont("myTabbedPanel.font")); // NOI18N
        tabbedMainPanel.setMaximumSize(new java.awt.Dimension(758, 366));
        tabbedMainPanel.setName("myTabbedPanel"); // NOI18N

        jToolBar1.setFloatable(false);
        jToolBar1.setName("jToolBar1"); // NOI18N

        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        btnSale.setFont(resourceMap.getFont("btnSale.font")); // NOI18N
        btnSale.setText(resourceMap.getString("btnSale.text")); // NOI18N
        btnSale.setFocusable(false);
        btnSale.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSale.setMargin(new java.awt.Insets(10, 12, 10, 12));
        btnSale.setName("btnSale"); // NOI18N
        btnSale.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSale.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnSaleMouseClicked(evt);
            }
        });
        btnSale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaleActionPerformed(evt);
            }
        });
        jToolBar1.add(btnSale);

        btnOrder.setBackground(resourceMap.getColor("btnOrder.background")); // NOI18N
        btnOrder.setFont(resourceMap.getFont("btnOrder.font")); // NOI18N
        btnOrder.setText(resourceMap.getString("btnOrder.text")); // NOI18N
        btnOrder.setFocusable(false);
        btnOrder.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnOrder.setMargin(new java.awt.Insets(10, 12, 10, 12));
        btnOrder.setName("btnOrder"); // NOI18N
        btnOrder.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnOrder.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnOrderMouseClicked(evt);
            }
        });
        jToolBar1.add(btnOrder);

        btnStock.setFont(resourceMap.getFont("btnStock.font")); // NOI18N
        btnStock.setText(resourceMap.getString("btnStock.text")); // NOI18N
        btnStock.setFocusable(false);
        btnStock.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnStock.setMargin(new java.awt.Insets(10, 12, 10, 12));
        btnStock.setName("btnStock"); // NOI18N
        btnStock.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnStock.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnStockMouseClicked(evt);
            }
        });
        jToolBar1.add(btnStock);

        btnCustomers.setFont(resourceMap.getFont("btnCustomers.font")); // NOI18N
        btnCustomers.setText(resourceMap.getString("btnCustomers.text")); // NOI18N
        btnCustomers.setFocusable(false);
        btnCustomers.setMargin(new java.awt.Insets(10, 12, 10, 12));
        btnCustomers.setName("btnCustomers"); // NOI18N
        btnCustomers.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCustomers.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnCustomersMouseClicked(evt);
            }
        });
        jToolBar1.add(btnCustomers);

        btnStatistics.setFont(resourceMap.getFont("btnStatistics.font")); // NOI18N
        btnStatistics.setText(resourceMap.getString("btnStatistics.text")); // NOI18N
        btnStatistics.setFocusable(false);
        btnStatistics.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnStatistics.setMargin(new java.awt.Insets(10, 12, 10, 12));
        btnStatistics.setName("btnStatistics"); // NOI18N
        btnStatistics.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnStatistics.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnStatisticsMouseClicked(evt);
            }
        });
        btnStatistics.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStatisticsActionPerformed(evt);
            }
        });
        jToolBar1.add(btnStatistics);

        lblLoginName.setText(resourceMap.getString("lblLoginName.text")); // NOI18N
        lblLoginName.setName("lblLoginName"); // NOI18N

        lblLoginStatus.setFont(resourceMap.getFont("lblLoginStatus.font")); // NOI18N
        lblLoginStatus.setText(resourceMap.getString("lblLoginStatus.text")); // NOI18N
        lblLoginStatus.setName("lblLoginStatus"); // NOI18N
        lblLoginStatus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblLoginStatusMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedMainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 758, Short.MAX_VALUE)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)
                .addGap(73, 73, 73)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblLoginName)
                    .addComponent(lblLoginStatus))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblLoginName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblLoginStatus)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tabbedMainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 378, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        btnFindCustomer.setText("Find kunde");
        btnFindCustomer.setName("btnFindCustomer"); // NOI18N
        btnFindCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFindCustomerActionPerformed(evt);
            }
        });
        fileMenu.add(btnFindCustomer);

        miShowLoginPanel.setText(resourceMap.getString("miShowLoginPanel.text")); // NOI18N
        miShowLoginPanel.setName("miShowLoginPanel"); // NOI18N
        miShowLoginPanel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miShowLoginPanelActionPerformed(evt);
            }
        });
        fileMenu.add(miShowLoginPanel);

        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
        jMenu1.setName("jMenu1"); // NOI18N

        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        jMenu1.add(jMenuItem1);

        menuBar.add(jMenu1);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 758, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 588, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents
    boolean popupVisible = false;

    private void mainPanelMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_mainPanelMouseClicked
    {//GEN-HEADEREND:event_mainPanelMouseClicked
    }//GEN-LAST:event_mainPanelMouseClicked

    private void btnSaleMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btnSaleMouseClicked
    {//GEN-HEADEREND:event_btnSaleMouseClicked
        ArrayList<JMenuItem> items = new ArrayList<JMenuItem>();

        JMenuItem item1 = new JMenuItem("Sælg ny bil");
        JMenuItem item2 = new JMenuItem("Find salg");
        ActionListener item1listener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                AddPane(new SellCarUI(mainView), "Opret salg");
            }
        };
        item1.addActionListener(item1listener);

        ActionListener item2listener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                AddPane(new FindSaleUI(mainView), "Find Salg");
            }
        };
        item2.addActionListener(item2listener);

        items.add(item1);
        items.add(item2);
        ShowPopupWindow(btnSale, items);
    }//GEN-LAST:event_btnSaleMouseClicked

    private void btnOrderMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btnOrderMouseClicked
    {//GEN-HEADEREND:event_btnOrderMouseClicked
        ArrayList<JMenuItem> SaleItems = new ArrayList<JMenuItem>();

        JMenuItem item1 = new JMenuItem("Opret Bestilling");
        JMenuItem item2 = new JMenuItem("Find Bestilling");

        ActionListener item1listener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                AddPane(new OrderCarUI(mainView), "Opret bestilling");
            }
        };
        item1.addActionListener(item1listener);

         ActionListener item2listener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                AddPane(new FindOrderUI(mainView), "Find bestilling");
            }
        };
        item2.addActionListener(item2listener);

        SaleItems.add(item1);
        SaleItems.add(item2);
        ShowPopupWindow(btnOrder, SaleItems);
    }//GEN-LAST:event_btnOrderMouseClicked

    private void btnSaleActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnSaleActionPerformed
    {//GEN-HEADEREND:event_btnSaleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnSaleActionPerformed

    private void btnFindCustomerActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnFindCustomerActionPerformed
    {//GEN-HEADEREND:event_btnFindCustomerActionPerformed
        ShowPickCustomerDialog();
    }//GEN-LAST:event_btnFindCustomerActionPerformed

    public void ShowPickCustomerDialog()
    {
        PickCustomerDialog dialog = new PickCustomerDialog(this.getFrame(), true, mainView);

        int x = (this.getFrame().getWidth() / 2) - (dialog.getWidth() / 2);
        int y = (this.getFrame().getHeight() / 2) - (dialog.getHeight() / 2);

        dialog.setLocation(x, y);
        dialog.setVisible(true);
    }

    private void btnStockMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btnStockMouseClicked
    {//GEN-HEADEREND:event_btnStockMouseClicked
        ArrayList<JMenuItem> items = new ArrayList<JMenuItem>();

        JMenuItem item1 = new JMenuItem("Se lager");
        ActionListener item1listener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                AddPane(new OrderCarUI(mainView), "Se lager");
            }
        };
        item1.addActionListener(item1listener);

        
        items.add(item1);
        ShowPopupWindow(btnStock, items);
    }//GEN-LAST:event_btnStockMouseClicked

    private void miShowLoginPanelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_miShowLoginPanelActionPerformed
    {//GEN-HEADEREND:event_miShowLoginPanelActionPerformed
        //JOptionPane.showMessageDialog(mainView.getFrame(), "Boom button");
        AddPane(new loginPanel(mainView), "Log in");
    }//GEN-LAST:event_miShowLoginPanelActionPerformed

    private void lblLoginStatusMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_lblLoginStatusMouseClicked
    {//GEN-HEADEREND:event_lblLoginStatusMouseClicked
        if (GlobalInstances.getInstance().isLoggedIn())
        {
            GlobalInstances.getInstance().logOut();
        }
        else
        {
            AddPane(new loginPanel(mainView), "Log ind");
        }
    }//GEN-LAST:event_lblLoginStatusMouseClicked

    private void btnStatisticsMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_btnStatisticsMouseClicked
    {//GEN-HEADEREND:event_btnStatisticsMouseClicked
        ArrayList<JMenuItem> items = new ArrayList<JMenuItem>();

        JMenuItem item1 = new JMenuItem("Vis Statistik");
        ActionListener item1listener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                AddPane(new StatisticUI(mainView), "Vis Statistik");
            }
        };
        item1.addActionListener(item1listener);

        items.add(item1);
        ShowPopupWindow(btnStatistics, items);
    }//GEN-LAST:event_btnStatisticsMouseClicked

    private void btnCustomersMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCustomersMouseClicked
         ArrayList<JMenuItem> Items = new ArrayList<JMenuItem>();

        JMenuItem item1 = new JMenuItem("Find kunde");
        ActionListener item1listener = new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                AddPane(new FindCustomerUI(mainView), "Find kunde");
            }
        };
        item1.addActionListener(item1listener);

        Items.add(item1);
        ShowPopupWindow(btnCustomers, Items);
    }//GEN-LAST:event_btnCustomersMouseClicked

    private void btnStatisticsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStatisticsActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btnStatisticsActionPerformed

    private void ShowPopupWindow(JButton button, ArrayList<JMenuItem> items)
    {
        menu = new JPopupMenu();

        for (JMenuItem item : items)
        {
            menu.add(item);
        }

        Point point = button.getLocation();

        menu.show(mainPanel, point.x + menu.getWidth(), point.y + btnSale.getHeight());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCustomers;
    private javax.swing.JButton btnOrder;
    private javax.swing.JButton btnSale;
    private javax.swing.JButton btnStatistics;
    private javax.swing.JButton btnStock;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel lblLoginName;
    private javax.swing.JLabel lblLoginStatus;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem miShowLoginPanel;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JTabbedPane tabbedMainPanel;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
}
